<?php

class AdminControl extends CommonControl
{
    public function __init()
    {

    }

    /**
     * 管理员登录
     */
    public function login()
    {
        $password = C('password');
        if ($password == $_POST['password']) {
            $_SESSION['admin'] = 1;
        }
        go("admin");
    }

    /**
     * 管理界面
     */
    public function admin()
    {
        if (!session("admin")) {
            go('Index/index');
        }
        $this->display();
    }

    /**
     * 下一题
     */
    public function next_problem(){
        M('problem')->save(array('pid'=>1,'team'=>''));
    }
    /**
     * 重新开始考试
     */
    public function reset_problem()
    {
        $db = M();
        $db->table('team')->where('1=1')->save(array(
            'point' => 0,
            'state' => 0
        ));
        $db->table("problem")->where('pid=1')->save(array('team'=>0,'num'=>0));
        $sql = "TRUNCATE session";
        $db->exe($sql);
        go("Index/index");
    }

    /**
     * 获得积分
     */
    public function get_point()
    {
        $db = M('team');
        $team = $db->all();
        $problem = $db->table("problem")->where('pid=1')->find();
        $group = $db->where("team ='{$problem['team']}'")->getField('title');
        $this->ajax(array('team' => $team, 'group' => $group,'num'=>$problem['num']));
    }

    /**
     * 加分
     */
    public function incr_point()
    {
        $db = M("team");
        $sql = "UPDATE team SET point=point+10 WHERE team='{$_GET['team']}'";
        $db->exe($sql);
        go('admin');
    }

    /**
     * 减分
     */
    public function decr_point()
    {
        $db = M("team");
        $sql = "UPDATE team SET point=point-10 WHERE team='{$_GET['team']}'";
        $db->exe($sql);
        go('admin');
    }


}